package com.wh.wisdomsite.xbox.server.filter;

import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.server.session.ServerSessionUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * api过滤器
 * @Package com.wh.wisdomsite.xbox.server.filter
 * @author 丁奕
 * @date 2018年9月20日 14:57:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 * @version V1.0
 */
public class ApiFilter implements Filter {


    private static final Log logger = LogFactory.getLog(ApiFilter.class);

    /**
     * 构造函数
     */
    public ApiFilter() {

    }

    /**
     * 销毁
     */
    public void destroy() {
        logger.info("destroy");
    }


    /**
     * 初始化
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    /**
     * 执行拦截
     * @param servletRequest
     * @param servletResponse
     * @param chain
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain){
        //获取HttpServletRequest
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //获取HttpServletResponse
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try{
            //设置ContentType
            response.setContentType("text/html; charset=UTF-8");
            //设置允许跨域请求
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            chain.doFilter(request, response);
        }catch  (Exception e){
            e.printStackTrace();
            logger.error("doFilter-->>Exception" + e.getMessage());
       }
    }
}
